<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.2">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/icon.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/icon.png">
  <link rel="mask-icon" href="/blog/images/icon.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"bgape002.gitee.io","root":"/blog/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="makefile相关概念 Makefile 一个工程中的源文件不计其数，其按类型、功能、模块分别放在若干目录中，Makefile定义了一系列的规则来指定哪些文件需要先编译，哪些文件需要后编译，哪些文件需要重新编译，甚至于进行更复杂的功能操作。   make 与Makefile的关系 make是一个命令工具，它解释Makefile中的指令。在Makefile文件中描述了整个工程所有文件的编译顺序、编">
<meta property="og:type" content="article">
<meta property="og:title" content="makefile">
<meta property="og:url" content="https://bgape002.gitee.io/2022/07/12/makefile/index.html">
<meta property="og:site_name" content="bgape002">
<meta property="og:description" content="makefile相关概念 Makefile 一个工程中的源文件不计其数，其按类型、功能、模块分别放在若干目录中，Makefile定义了一系列的规则来指定哪些文件需要先编译，哪些文件需要后编译，哪些文件需要重新编译，甚至于进行更复杂的功能操作。   make 与Makefile的关系 make是一个命令工具，它解释Makefile中的指令。在Makefile文件中描述了整个工程所有文件的编译顺序、编">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://bgape002.gitee.io/blog/images/makefile_build_stage.png">
<meta property="article:published_time" content="2022-07-12T05:38:57.000Z">
<meta property="article:modified_time" content="2022-11-03T08:49:23.223Z">
<meta property="article:author" content="bgape002">
<meta property="article:tag" content="makefile">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://bgape002.gitee.io/blog/images/makefile_build_stage.png">

<link rel="canonical" href="https://bgape002.gitee.io/2022/07/12/makefile/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>makefile | bgape002</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">bgape002</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">淡泊明志，宁静致远</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">66</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">27</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">61</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://bgape002.gitee.io/2022/07/12/makefile/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/head.png">
      <meta itemprop="name" content="bgape002">
      <meta itemprop="description" content="mail: bgape002@163.com">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="bgape002">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          makefile
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-07-12 13:38:57" itemprop="dateCreated datePublished" datetime="2022-07-12T13:38:57+08:00">2022-07-12</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-11-03 16:49:23" itemprop="dateModified" datetime="2022-11-03T16:49:23+08:00">2022-11-03</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/makefile/" itemprop="url" rel="index"><span itemprop="name">makefile</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/blog/2022/07/12/makefile/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/blog/2022/07/12/makefile/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="makefile相关概念"><a href="#makefile相关概念" class="headerlink" title="makefile相关概念"></a>makefile相关概念</h3><ul>
<li><code>Makefile</code><ul>
<li>一个工程中的源文件不计其数，其按类型、功能、模块分别放在若干目录中，<code>Makefile</code>定义了一系列的规则来指定哪些文件需要先编译，哪些文件需要后编译，哪些文件需要重新编译，甚至于进行更复杂的功能操作。</li>
</ul>
</li>
<li><code>make </code>与<code>Makefile</code>的关系<ul>
<li><code>make</code>是一个命令工具，它解释<code>Makefile</code>中的指令。在<code>Makefile</code>文件中描述了整个工程所有文件的编译顺序、编译规则。</li>
</ul>
</li>
<li><code>Makefile</code>命名规则<ul>
<li><code>Makefile</code>或<code>makefile</code></li>
</ul>
</li>
<li><code>Cmake</code><ul>
<li><code>Cmake</code>是一个跨平台的安装工具，可以用简单的语句来描述所有平台的安装（编译过程）。它能够输出各种各样的<code>Makefile</code>或<code>project</code>文件，能测试编译所支持的C++特性，类似UNIX下的<code>automake</code>。只是<code>Cmake</code>的组态取名为<code>CmakeList.txt</code>。<code>Cmake</code>并不直接构建出最终的软件，而是产生标准的建构文档（如 Unix的<code>Makefile</code>或Windows Visual C++的<code>project/workspace</code>），然后再依一般的构建方式使用。</li>
</ul>
</li>
<li><code>Cmake </code>与<code>CMakeList</code>的关系<ul>
<li><code>Cmake </code>是一个命令工具，可以生成<code>Makefile</code>，但也要根据<code>CMakeList.txt</code>中内容来生成，<code>CMakeList</code>是写给<code>Cmake </code>的规则。</li>
</ul>
</li>
</ul>
<span id="more"></span>

<h3 id="简单运行"><a href="#简单运行" class="headerlink" title="简单运行"></a>简单运行</h3><ul>
<li><p><strong>目标：</strong>一般是指编译的目标，也可以是一个动作</p>
</li>
<li><p><strong>依赖：</strong></p>
<ul>
<li><p>执行当前目标所要依赖的先项，包括其它目标，某个具体文件或库等</p>
</li>
<li><p>一个目标可以有多个依赖</p>
</li>
</ul>
</li>
<li><p><strong>命令：</strong></p>
<ul>
<li>该目标下要执行的具体命令，可以没有，也可以有多条</li>
<li>多条时，每条命令一行</li>
</ul>
</li>
<li><p><strong>make 常用选项</strong></p>
<blockquote>
<p><strong>make [-f file] [options] [target]</strong></p>
</blockquote>
<ul>
<li><strong>make默认在当前目录中寻找<code>GUNmakefile、makefile、Makefile</code>文件作为make的输入文件</strong></li>
<li><strong>没有指定目标时，默认使用第一个目标；如果指定目标，则执行对应的命令。</strong></li>
<li><strong>-f</strong>        可以指定除上述文件名之外的文件作为输入文件</li>
<li><strong>-v</strong>        显示版本号</li>
<li><strong>-n</strong>        只输出命令，但并不执行，一般用来测试</li>
<li><strong>-s</strong>         只执行命令，但不显示具体命令，此处可在命令中用@符抑制命令输出</li>
<li><strong>-w</strong>        显示执行前执行后的路径</li>
<li><strong><code>-C dir</code></strong>   指定makefile所在的目录</li>
</ul>
</li>
<li><p><strong>参数传递</strong></p>
<ul>
<li><p>make指令后跟参数，会同步覆盖文件中已定义的参数</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 命令行指令</span></span><br><span class="line">make HOST=172.0.0.6</span><br><span class="line"></span><br><span class="line"><span class="comment"># 文件中有HOST变量，则会替换文件中对应的变量；文件中没有，则新增HOST</span></span><br><span class="line">...</span><br><span class="line">HOST = 172.0.0.0</span><br><span class="line">...</span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p><strong>分号和反斜线</strong></p>
<ul>
<li>分号是分隔符，反斜线是换行符。像if 或for 语句要写在一行时，必须使用分号进行分割，如需进行换行则使用反斜线进行换行</li>
</ul>
</li>
</ul>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># @为抑制输出，类似于make -s，打印时印制命令本身信息打印</span></span><br><span class="line"><span class="comment"># b c是a的依赖</span></span><br><span class="line"></span><br><span class="line"><span class="section">a:b c</span></span><br><span class="line">	@echo <span class="string">&quot;hello world a&quot;</span></span><br><span class="line">	@ls ./</span><br><span class="line"><span class="section">b:</span></span><br><span class="line">	@echo <span class="string">&quot;hello world b&quot;</span></span><br><span class="line"><span class="section">c:</span></span><br><span class="line">	@echo <span class="string">&quot;hello world c&quot;</span></span><br><span class="line">	</span><br><span class="line"><span class="comment"># make </span></span><br><span class="line"><span class="comment"># 默认第一个目标，即a: b c 会依次执行依赖b c，再执行a</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># make c</span></span><br><span class="line"><span class="comment"># 会执行目标c,即输出 hello world c</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="gcc-g-编译流程"><a href="#gcc-g-编译流程" class="headerlink" title="gcc/g++编译流程"></a><code>gcc/g++</code>编译流程</h3><p><code>gcc -lstdc++ main.cpp</code>直接从源代码到目标可执行文件，该过程拆解如下：</p>
<ul>
<li><strong>预处理：</strong><code>gcc -E main.cpp</code>得到<code>main.ii</code></li>
<li><strong>编译：</strong><code>gcc -S main.ii</code>得到汇编文件<code>main.s</code></li>
<li><strong>汇编：</strong><code>gcc -c main.s</code>得到二进制文件<code>main.o(.obj)</code></li>
<li><strong>链接：</strong><code>gcc -lstdc++ main.o</code>得到可执行文件<code>a.out</code></li>
</ul>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># calc:</span></span><br><span class="line"><span class="comment">#	gcc add.cpp sub.cpp multi.cpp calc.cpp -o calc</span></span><br><span class="line"><span class="comment"># 这样写不好，如果代码有改动，需要全部重新编译</span></span><br><span class="line"></span><br><span class="line"><span class="section">calc:add.o sub.o multi.o</span></span><br><span class="line">	gcc add.o sub.o multi.o calc.cpp -o calc</span><br><span class="line">	</span><br><span class="line"><span class="section">add.o:add.cpp</span></span><br><span class="line">	gcc -c add.cpp -o add.o</span><br><span class="line">	</span><br><span class="line"><span class="section">sub.o:sub.cpp</span></span><br><span class="line">	gcc -c sub.cpp -o sub.o</span><br><span class="line">	</span><br><span class="line"><span class="section">multi.o:multi.cpp</span></span><br><span class="line">	gcc -c multi.cpp -o multi.o</span><br><span class="line">	</span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	rm -rf *.o calc</span><br><span class="line"><span class="comment"># 分开来写，保证只编译有改动的代码</span></span><br><span class="line"><span class="comment"># “第一次编译两小时，改动后二次编译五分钟”</span></span><br></pre></td></tr></table></figure>



<h3 id="makefile中的变量"><a href="#makefile中的变量" class="headerlink" title="makefile中的变量"></a><code>makefile</code>中的变量</h3><ul>
<li><p><strong>自动化变量</strong></p>
<table>
<thead>
<tr>
<th align="center"><strong>$</strong>*</th>
<th align="center">表示目标文件的名称，不包含扩展名</th>
</tr>
</thead>
<tbody><tr>
<td align="center"><strong>$@</strong></td>
<td align="center"><strong>表示目标文件的名称，包含扩展名</strong></td>
</tr>
<tr>
<td align="center"><strong>$&lt;</strong></td>
<td align="center"><strong>表示第一个依赖文件的名称</strong></td>
</tr>
<tr>
<td align="center"><strong>$?</strong></td>
<td align="center"><strong>所有时间戳比目标文件晚的依赖文件，以空格分隔</strong></td>
</tr>
<tr>
<td align="center"><strong>$+</strong></td>
<td align="center"><strong>所有的依赖文件，以空格分隔</strong></td>
</tr>
<tr>
<td align="center"><strong>$^</strong></td>
<td align="center"><strong>所有不重复的依赖文件，以空格分隔</strong></td>
</tr>
<tr>
<td align="center"><strong>$%</strong></td>
<td align="center"><strong>当目标文件是一个静态库文件时，代表静态库的一个成员名</strong></td>
</tr>
</tbody></table>
<blockquote>
<p>GNU make 中在这些变量中加入字符 “D” 或者 “F” 就形成了一系列变种的自动化变量，这些自动化变量可以对文件的名称进行操作。</p>
</blockquote>
<table>
<thead>
<tr>
<th>$(@D)</th>
<th>表示文件的目录部分（不包括斜杠）。如果 <code>$@</code> 表示的是 <code>dir/foo.o</code> 那么 <code>$(@D)</code> 表示的值就是 <code>dir</code>。如果 <code>$@</code> 不存在斜杠（文件在当前目录下），其值就是 “.”。</th>
</tr>
</thead>
<tbody><tr>
<td><strong>$(@F)</strong></td>
<td><strong>表示的是文件除目录外的部分（实际的文件名）。如果 <code>$@</code> 表示的是 <code>dir/foo.o</code>，那么 <code>$@F</code>表示的值为 <code>foo.o</code>。</strong></td>
</tr>
<tr>
<td><strong>$(*D) $(*F)</strong></td>
<td><strong>分别代表 “茎” 中的目录部分和文件名部分</strong></td>
</tr>
<tr>
<td><strong>$(%D) $(%F)</strong></td>
<td><strong>当以 <code>archive(member)</code>形式静态库为目标时，分别表示库文件成员 “member” 名中的目录部分和文件名部分。踏进对这种新型时的目标有效。</strong></td>
</tr>
<tr>
<td><strong>$(&lt;D) $(&lt;F)</strong></td>
<td><strong>表示第一个依赖文件的目录部分和文件名部分。</strong></td>
</tr>
<tr>
<td><strong>$(^D) $(^F)</strong></td>
<td><strong>分别表示所有不重复依赖文件的目录部分和文件部分。</strong></td>
</tr>
<tr>
<td><strong>$(+D) $(+F)</strong></td>
<td><strong>分别表示所有的依赖文件的目录部分和文件部分。</strong></td>
</tr>
<tr>
<td><strong>$(?D) $(?F)</strong></td>
<td><strong>分别表示更新的依赖文件的目录部分和文件名部分。</strong></td>
</tr>
</tbody></table>
</li>
<li><p><strong>系统常量</strong>（可用<code>make -p</code>查看）</p>
<table>
<thead>
<tr>
<th align="center">AS</th>
<th align="center">汇编程序的名称</th>
<th align="center">默认 as</th>
</tr>
</thead>
<tbody><tr>
<td align="center"><strong>CC</strong></td>
<td align="center"><strong>C编译器名称</strong></td>
<td align="center"><strong>默认 cc</strong></td>
</tr>
<tr>
<td align="center"><strong>CPP</strong></td>
<td align="center"><strong>C预编译器名称</strong></td>
<td align="center"><strong>默认 cc -E</strong></td>
</tr>
<tr>
<td align="center"><strong>CXX</strong></td>
<td align="center"><strong>C++ 编译器名称</strong></td>
<td align="center"><strong>默认 g++</strong></td>
</tr>
<tr>
<td align="center"><strong>RM</strong></td>
<td align="center"><strong>文件删除程序别名</strong></td>
<td align="center"><strong>默认 rm -f</strong></td>
</tr>
</tbody></table>
<p>​    </p>
</li>
<li><p><strong>自定义变量</strong></p>
<blockquote>
<p>定义：<strong>变量名=变量值</strong></p>
<p>使用：<strong>$(变量名） 或   ${变量名}</strong></p>
</blockquote>
</li>
<li><p><strong>$和$$</strong></p>
<ul>
<li>单$表示makefile定义变量的值，双$表示引用shell命令中定义变量的值</li>
</ul>
</li>
</ul>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 自定义变量</span></span><br><span class="line"></span><br><span class="line">OBJ=add.o sub.o multi.o cpp.o</span><br><span class="line">TARGET=calc</span><br><span class="line"></span><br><span class="line"><span class="variable">$(TARGET)</span>:<span class="variable">$(OBJ)</span></span><br><span class="line">	gcc <span class="variable">$(OBJ)</span> -o <span class="variable">$(TARGET)</span></span><br><span class="line">	</span><br><span class="line"><span class="section">add.o:add.cpp</span></span><br><span class="line">	gcc -c add.cpp -o add.o</span><br><span class="line">	</span><br><span class="line"><span class="section">sub.o:sub.cpp</span></span><br><span class="line">	gcc -c sub.cpp -o sub.o</span><br><span class="line">	</span><br><span class="line"><span class="section">multi.o:multi.cpp</span></span><br><span class="line">	gcc -c multi.cpp -o multi.o</span><br><span class="line">	</span><br><span class="line"><span class="section">calc.o:calc.cpp</span></span><br><span class="line">	gcc -c calc.o -o calc.o</span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	rm -rf *.o calc</span><br></pre></td></tr></table></figure>



<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 自动化变量</span></span><br><span class="line"><span class="comment"># $^  所有不重复的依赖文件，以空格分隔</span></span><br><span class="line"><span class="comment"># $@  目标文件的完整名称</span></span><br><span class="line"></span><br><span class="line">OBJ=add.o sub.o multi.o cpp.o</span><br><span class="line">TARGET=calc</span><br><span class="line"></span><br><span class="line"><span class="variable">$(TARGET)</span>:<span class="variable">$(OBJ)</span></span><br><span class="line">	gcc <span class="variable">$^</span> -o <span class="variable">$@</span></span><br><span class="line">	</span><br><span class="line"><span class="section">add.o:add.cpp</span></span><br><span class="line">	gcc -c <span class="variable">$^</span> -o <span class="variable">$@</span> </span><br><span class="line">	</span><br><span class="line"><span class="section">sub.o:sub.cpp</span></span><br><span class="line">	gcc -c <span class="variable">$^</span> -o <span class="variable">$@</span> </span><br><span class="line">	</span><br><span class="line"><span class="section">multi.o:multi.cpp</span></span><br><span class="line">	gcc -c <span class="variable">$^</span> -o <span class="variable">$@</span> </span><br><span class="line">	</span><br><span class="line"><span class="section">calc.o:calc.cpp</span></span><br><span class="line">	gcc -c <span class="variable">$^</span> -o <span class="variable">$@</span> </span><br><span class="line">	</span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	rm -rf *.o <span class="variable">$(TARGET)</span></span><br></pre></td></tr></table></figure>



<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 系统常量，使用系统常量的好处是可以跨平台使用，makefile会自己寻找对应的指令器</span></span><br><span class="line"><span class="comment"># CXX  C++ 编译器名称     默认 g++</span></span><br><span class="line"><span class="comment"># RM   文件删除程序别名    默认 rm -f</span></span><br><span class="line"></span><br><span class="line">OBJ=add.o sub.o multi.o cpp.o</span><br><span class="line">TARGET=calc</span><br><span class="line"></span><br><span class="line"><span class="variable">$(TARGET)</span>:<span class="variable">$(OBJ)</span></span><br><span class="line">	<span class="variable">$(CXX)</span> <span class="variable">$^</span> -o <span class="variable">$@</span></span><br><span class="line">	</span><br><span class="line"><span class="section">add.o:add.cpp</span></span><br><span class="line">	<span class="variable">$(CXX)</span> -c <span class="variable">$^</span> -o <span class="variable">$@</span> </span><br><span class="line">	</span><br><span class="line"><span class="section">sub.o:sub.cpp</span></span><br><span class="line">	<span class="variable">$(CXX)</span> -c <span class="variable">$^</span> -o <span class="variable">$@</span> </span><br><span class="line">	</span><br><span class="line"><span class="section">multi.o:multi.cpp</span></span><br><span class="line">	<span class="variable">$(CXX)</span> -c <span class="variable">$^</span> -o <span class="variable">$@</span> </span><br><span class="line">	</span><br><span class="line"><span class="section">calc.o:calc.cpp</span></span><br><span class="line">	<span class="variable">$(CXX)</span> -c <span class="variable">$^</span> -o <span class="variable">$@</span> </span><br><span class="line">	</span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	<span class="variable">$(RM)</span> -rf *.o <span class="variable">$(TARGET)</span></span><br><span class="line">	</span><br></pre></td></tr></table></figure>

<h3 id="makefile中的伪目标和模式匹配"><a href="#makefile中的伪目标和模式匹配" class="headerlink" title="makefile中的伪目标和模式匹配"></a><code>makefile</code>中的伪目标和模式匹配</h3><ul>
<li><p><strong>伪目标</strong> <code>.PHONY:clean</code></p>
<ul>
<li>声明目标为<strong>伪目标</strong>之后，<code>makefile</code>将不会判断目标是否存在或该目标是否需要更新</li>
<li>目标“clean”就被声明为一个伪目标，无论在当前目录下是否存在“clean”这个<br>文件。我们输入“make clean”之后。“rm”命令都会被执行。<strong>不用担心由于目标和文件名重名而使我们的期望失败</strong>。</li>
</ul>
</li>
<li><p><strong>模式匹配</strong></p>
<table>
<thead>
<tr>
<th align="center"><code>%.o:%.cpp</code></th>
<th align="center">.o依赖于对应的<code>.cpp</code></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><strong><code>wildcard</code></strong></td>
<td align="center"><code>$(wildcard ./*.cpp)</code>   <strong>获取当前目录下所有的.<code>cpp</code>文件</strong></td>
</tr>
<tr>
<td align="center"><strong><code>patsubst</code></strong></td>
<td align="center"><code>$(patsubst %.cpp, %.o, ./*.cpp) </code>      <strong>将对应的<code>cpp</code>文件名替换成.o文件名</strong></td>
</tr>
</tbody></table>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 模式匹配</span></span><br><span class="line"><span class="comment"># %目标:%依赖     目标和依赖相同的部分，可以用%来通配      如%.o:%.cpp </span></span><br><span class="line"><span class="comment"># $(patsubst %.cpp, %.o, `$(wildcard ./*.cpp)`)      将对应的cpp文件名替换成.o文件名</span></span><br><span class="line"></span><br><span class="line"><span class="meta"><span class="keyword">.PHONY</span>:clean</span></span><br><span class="line"></span><br><span class="line">OBJ=<span class="variable">$(<span class="built_in">patsubst</span> %.cpp, %.o, ./*.cpp)</span></span><br><span class="line">TARGET=calc</span><br><span class="line"></span><br><span class="line"><span class="variable">$(TARGET)</span>:<span class="variable">$(OBJ)</span></span><br><span class="line">	<span class="variable">$(CXX)</span> <span class="variable">$^</span> -o <span class="variable">$@</span></span><br><span class="line">	</span><br><span class="line"><span class="section">%.o:%.cpp</span></span><br><span class="line">	<span class="variable">$(CXX)</span> -c <span class="variable">$^</span> -o <span class="variable">$@</span> </span><br><span class="line">	</span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	<span class="variable">$(RM)</span> -rf *.o <span class="variable">$(TARGET)</span></span><br><span class="line">	</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="makefile运行流程"><a href="#makefile运行流程" class="headerlink" title="makefile运行流程"></a><code>makefile</code>运行流程</h3><ul>
<li>目标是用最新的依赖生成的。<strong>保证依赖文件有变动，则会重新生成目标。</strong></li>
</ul>
<p><img src="/blog/images/makefile_build_stage.png"></p>
<h3 id="makefile编译动态链接库"><a href="#makefile编译动态链接库" class="headerlink" title="makefile编译动态链接库"></a><code>makefile</code>编译动态链接库</h3><ul>
<li><strong>动态链接库：</strong>不会把代码编译到二进制文件中，而是在<strong>运行时才去加载</strong>，所以只需要维护一个地址<ul>
<li><code>-fPIC</code>     产生位置无关的代码</li>
<li><code>-shared</code>    共享</li>
<li><code>-l</code>    指定动态库</li>
<li><code>-I</code>    指定头文件目录，默认当前目录</li>
<li><code>-L</code>    手动指定库文件搜索目录，默认只链接共享目录</li>
</ul>
</li>
<li>动态链接库好处是<strong>程序可以和库文件分离，可以分别发版，然后库文件可以被多处共享</strong><ul>
<li><strong>动态</strong>    运行时才去加载   动态加载</li>
<li><strong>链接</strong>    指库文件和二进制程序分离，用某种特殊手段维护两者之间的关系</li>
<li><strong>库</strong>    库文件  <code>.dll   .so</code></li>
</ul>
</li>
</ul>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">test:libSoTest.so</span></span><br><span class="line">	<span class="variable">$(CXX)</span> -lSoTest -L./test.cpp -o test</span><br><span class="line">	cp libSoTest.so /usr/local/lib/</span><br><span class="line">	</span><br><span class="line"><span class="section">libSoTest.so:</span></span><br><span class="line">	<span class="variable">$(CXX)</span> -fPIC -shared SoTest.cpp -o libSoTest.so</span><br><span class="line">	</span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	<span class="variable">$(RM)</span> *.so test</span><br></pre></td></tr></table></figure>



<h3 id="makefile编译静态链接库"><a href="#makefile编译静态链接库" class="headerlink" title="makefile编译静态链接库"></a><code>makefile</code>编译静态链接库</h3><ul>
<li><p><strong>静态链接库 <code>.lib  .a</code>:</strong>    会把库中的代码编译到二进制文件中，当程序编译完成后，该库文件可以删除</p>
<p>而动态链接库必须与程序同步部署，要保证程序能加载得到库文件</p>
</li>
<li><p>与动态库相比，静态库可以不用部署（已经被加载到程序里里面），而且运行时速度更快</p>
<p>但会导致程序体积更大，并且库中的内容如果有更新，则需要重新编译生成程序</p>
</li>
</ul>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">TARGET=main</span><br><span class="line">LDFLAGS=-L./001 -L./002</span><br><span class="line">LIBS=lSoTest -laTest</span><br><span class="line"></span><br><span class="line"><span class="variable">$(TARGET)</span>:</span><br><span class="line">	<span class="variable">$(CXX)</span> main.cpp -o <span class="variable">$(TARGRT)</span></span><br><span class="line">	</span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	<span class="variable">$(RM)</span> <span class="variable">$(TARGET)</span></span><br></pre></td></tr></table></figure>



<h3 id="makefile中通用部分做公共头文件"><a href="#makefile中通用部分做公共头文件" class="headerlink" title="makefile中通用部分做公共头文件"></a><code>makefile</code>中通用部分做公共头文件</h3><ul>
<li><p>=     是终值赋值（往后递归查找），make会将整个makefile展开后，再决定变量的值。<code>Y=$(Y)</code>会报错</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">x = foo</span><br><span class="line">y = <span class="variable">$(x)</span>bar</span><br><span class="line"></span><br><span class="line"><span class="section">a:</span></span><br><span class="line">	@echo <span class="variable">$(x)</span> <span class="variable">$(y)</span></span><br><span class="line">	</span><br><span class="line">x = xyz</span><br><span class="line"></span><br><span class="line"><span class="comment"># output</span></span><br><span class="line"><span class="comment"># xyz xyzbar</span></span><br><span class="line"><span class="comment">#</span></span><br></pre></td></tr></table></figure></li>
<li><p>:=    是覆盖之前的值，变量的值取定于它在<code>makefile</code>中的位置。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">x := foo</span><br><span class="line">y := <span class="variable">$(x)</span>bar</span><br><span class="line"></span><br><span class="line"><span class="section">a:</span></span><br><span class="line">	@echo <span class="variable">$(x)</span> <span class="variable">$(y)</span></span><br><span class="line">	</span><br><span class="line">x := xyz</span><br><span class="line"></span><br><span class="line"><span class="comment"># output</span></span><br><span class="line"><span class="comment"># xyz foobar</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#########</span></span><br><span class="line">x := foo</span><br><span class="line">y = <span class="variable">$(x)</span>bar</span><br><span class="line"></span><br><span class="line"><span class="section">a:</span></span><br><span class="line">	@echo <span class="variable">$(x)</span> <span class="variable">$(y)</span></span><br><span class="line">	</span><br><span class="line">x := xyz</span><br><span class="line"></span><br><span class="line"><span class="comment"># output</span></span><br><span class="line"><span class="comment"># xyz xyzbar</span></span><br><span class="line"><span class="comment">#</span></span><br></pre></td></tr></table></figure></li>
<li><p>?=    是如果没有被赋值过就赋予等号后面的值</p>
</li>
<li><p>+=    是添加等号后面的值</p>
</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// a.cpp</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">funca</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    cout &lt;&lt; &quot;Hello, aaa!&quot; &lt;&lt; endl</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/////////////</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// b.cpp</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">funcb</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    cout &lt;&lt; &quot;Hello, bbb!&quot; &lt;&lt; endl</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/////////////</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// c.cpp</span></span><br><span class="line"><span class="function"><span class="keyword">extern</span> <span class="type">int</span> <span class="title">funca</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">extern</span> <span class="type">int</span> <span class="title">funcb</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    <span class="title">funca</span><span class="params">()</span></span>;</span><br><span class="line">	<span class="built_in">funcb</span>();</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># makefile</span></span><br><span class="line"><span class="meta"><span class="keyword">.PHONY</span>:clean</span></span><br><span class="line"></span><br><span class="line">TARGET=c</span><br><span class="line">OBJ=a.o b.o c.o</span><br><span class="line"></span><br><span class="line"><span class="variable">$(TARGET)</span>:<span class="variable">$(OBJ)</span></span><br><span class="line">	<span class="variable">$(CXX)</span> <span class="variable">$^</span> -o <span class="variable">$@</span></span><br><span class="line">	</span><br><span class="line"><span class="comment"># %.o:%.cpp</span></span><br><span class="line"><span class="comment">#	 gcc -c $^ -o $@ </span></span><br><span class="line"></span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	<span class="variable">$(RM)</span> <span class="variable">$(TARGET)</span> <span class="variable">$(OBJ)</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>include  引用公用头文件</strong></p>
</blockquote>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># ../makefile</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 公共</span></span><br><span class="line"><span class="meta"><span class="keyword">.PHONY</span>:clean</span></span><br><span class="line">SOURCE=<span class="variable">$(<span class="built_in">wildcard</span> ./*.cpp ./*.c)</span></span><br><span class="line">OBJ = <span class="variable">$(<span class="built_in">patsubst</span> %.cpp,%.o,<span class="variable">$(SOURCE)</span>)</span></span><br><span class="line">OBJ:=<span class="variable">$(<span class="built_in">patsubst</span> %.c,%.o,<span class="variable">$(OBJ)</span>)</span></span><br><span class="line"></span><br><span class="line"><span class="variable">$(TARGET)</span>:<span class="variable">$(OBJ)</span></span><br><span class="line">	<span class="variable">$(CXX)</span> <span class="variable">$^</span> -o <span class="variable">$@</span></span><br><span class="line">	</span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	<span class="variable">$(RM)</span> <span class="variable">$(TARGET)</span> <span class="variable">$(OBJ)</span></span><br><span class="line">	</span><br><span class="line"><span class="comment">############</span></span><br><span class="line"><span class="comment"># ./makefile</span></span><br><span class="line"></span><br><span class="line">TARGET=c</span><br><span class="line"></span><br><span class="line"><span class="keyword">include</span> ../makefile</span><br></pre></td></tr></table></figure>



<h3 id="makefile中调用shell命令"><a href="#makefile中调用shell命令" class="headerlink" title="makefile中调用shell命令"></a><code>makefile</code>中调用<code>shell</code>命令</h3><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">FILE=newfile</span><br><span class="line">A:=<span class="variable">$(<span class="built_in">shell</span> ls ../)</span></span><br><span class="line">B:=<span class="variable">$(<span class="built_in">shell</span> pwd)</span></span><br><span class="line">C:=<span class="variable">$(<span class="built_in">shell</span> <span class="built_in">if</span> [! -f <span class="variable">$(FILE)</span>];then touch <span class="variable">$(FILE)</span>;fi;)</span></span><br><span class="line"></span><br><span class="line"><span class="section">a:</span></span><br><span class="line">	echo <span class="variable">$(A)</span></span><br><span class="line">	echo <span class="variable">$(B)</span></span><br><span class="line">	echo <span class="variable">$(C)</span></span><br><span class="line">	</span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	<span class="variable">$(RM)</span> <span class="variable">$(FILE)</span></span><br></pre></td></tr></table></figure>



<h3 id="makefile中的嵌套调用"><a href="#makefile中的嵌套调用" class="headerlink" title="makefile中的嵌套调用"></a><code>makefile</code>中的嵌套调用</h3><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># </span></span><br><span class="line"><span class="comment"># $$表示展开shell中的变量</span></span><br><span class="line"></span><br><span class="line"><span class="meta"><span class="keyword">.PHONY</span>:001 002</span></span><br><span class="line"></span><br><span class="line">DIR=001 002</span><br><span class="line"></span><br><span class="line"><span class="section">all:<span class="variable">$(DIR)</span></span></span><br><span class="line"></span><br><span class="line"><span class="variable">$(DIR)</span>:</span><br><span class="line">	make -C <span class="variable">$@</span></span><br><span class="line">	</span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	echo <span class="variable">$(<span class="built_in">shell</span> for <span class="built_in">dir</span> in <span class="variable">$(DIR)</span>;do make -C $$<span class="built_in">dir</span> clean;done)</span></span><br></pre></td></tr></table></figure>



<h3 id="makefile-中的条件判断"><a href="#makefile-中的条件判断" class="headerlink" title="makefile 中的条件判断"></a><code>makefile</code> 中的条件判断</h3><ul>
<li><code>ifeq</code>    判断是否相等，返回<code>bool</code>值</li>
<li><code>ifneq</code>    判断是否不相等，返回<code>bool</code>值</li>
<li><code>ifdef</code>    判断变量是否存在，返回<code>bool</code>值</li>
<li><code>ifndef</code>    判断变量是否不存在，返回<code>bool</code>值</li>
</ul>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 没有elif的用法，但可以用嵌套实现</span></span><br><span class="line"><span class="comment"># ifeq等与条件之间要保留  空格</span></span><br><span class="line"></span><br><span class="line">A=321</span><br><span class="line"></span><br><span class="line"><span class="keyword">ifeq</span> (<span class="variable">$(A)</span>,123)</span><br><span class="line">	RS1:=yes</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">	<span class="keyword">ifeq</span> (<span class="variable">$(A)</span>,321)</span><br><span class="line">		RS1:=no</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		RS1:=no-123-321</span><br><span class="line">	<span class="keyword">endif</span></span><br><span class="line"><span class="keyword">endif</span></span><br><span class="line"><span class="comment">### output</span></span><br><span class="line"><span class="comment"># no</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">###</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">ifdef</span> FLAG</span><br><span class="line">	FLAG:=default-flag</span><br><span class="line"><span class="keyword">endif</span></span><br><span class="line"><span class="comment">### output</span></span><br><span class="line"><span class="comment"># default-flag</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">###</span></span><br><span class="line"></span><br><span class="line"><span class="section">all:</span></span><br><span class="line">	echo <span class="variable">$(RS1)</span></span><br><span class="line">	echo flag=<span class="variable">$(FLAG)</span></span><br></pre></td></tr></table></figure>



<h3 id="makefile中的循环"><a href="#makefile中的循环" class="headerlink" title="makefile中的循环"></a><code>makefile</code>中的循环</h3><ul>
<li><code>makefile</code>中只有一个循环 <code>foreach</code></li>
</ul>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">TARGET=a b c d</span><br><span class="line"></span><br><span class="line"><span class="section">all:</span></span><br><span class="line">	touch <span class="variable">$(<span class="built_in">foreach</span> v,<span class="variable">$(TARGET)</span>,$v.txt)</span>   <span class="comment"># 在GNU make下支持</span></span><br><span class="line">	<span class="comment"># 其他平台的遍历可用shell for语句</span></span><br><span class="line">	<span class="comment"># for v in $(TARGET);do touch $$v.txt;done    # 1</span></span><br><span class="line">	<span class="variable">$(<span class="built_in">shell</span> for v in <span class="variable">$(TARGET)</span>;do touch $$v.txt;done)</span>  <span class="comment"># 2</span></span><br><span class="line">	</span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	<span class="variable">$(RM)</span> -rf <span class="variable">$(TARGET)</span> *txt</span><br></pre></td></tr></table></figure>



<h3 id="makefile中的自定义函数"><a href="#makefile中的自定义函数" class="headerlink" title="makefile中的自定义函数"></a><code>makefile</code>中的自定义函数</h3><ul>
<li>自定义函数，不是真正的函数，<strong>本质上是多行命令</strong></li>
<li>没有返回值</li>
</ul>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">A:=123</span><br><span class="line">B:=<span class="variable">$(A)</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">define</span> FUNC1</span><br><span class="line">    echo func1</span><br><span class="line">    echo <span class="variable">$(A)</span> <span class="variable">$(B)</span></span><br><span class="line"><span class="keyword">endef</span></span><br><span class="line"></span><br><span class="line">A:=456</span><br><span class="line"><span class="section">all:</span></span><br><span class="line">    <span class="variable">$(<span class="built_in">call</span> FUNC1)</span></span><br><span class="line">    echo <span class="variable">$(A)</span> <span class="variable">$(B)</span></span><br><span class="line"></span><br><span class="line">A:=789</span><br><span class="line"><span class="comment">### output</span></span><br><span class="line"><span class="comment"># func1 </span></span><br><span class="line"><span class="comment"># 789 123</span></span><br><span class="line"><span class="comment"># 789 123</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">###</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 传参  $(0)是函数名，$(1)是第一个变量</span></span><br><span class="line">A:=123</span><br><span class="line"></span><br><span class="line"><span class="keyword">define</span> FUNC2</span><br><span class="line">    echo $(0)</span><br><span class="line">    echo $(1) $(2)</span><br><span class="line"><span class="keyword">endef</span></span><br><span class="line"></span><br><span class="line">A:=456</span><br><span class="line"><span class="section">all:</span></span><br><span class="line">    <span class="variable">$(<span class="built_in">call</span> FUNC2,abc,def)</span></span><br><span class="line"></span><br><span class="line">A:=789</span><br><span class="line"><span class="comment">### output</span></span><br><span class="line"><span class="comment"># FUNC2 </span></span><br><span class="line"><span class="comment"># abc def</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">###</span></span><br></pre></td></tr></table></figure>



<h3 id="make-install的实现"><a href="#make-install的实现" class="headerlink" title="make install的实现"></a><code>make install</code>的实现</h3><p><strong>项目编译安装过程：</strong></p>
<ul>
<li><code>make</code><ol>
<li>将源文件编译成二进制可执行文件（包括各种库文件）</li>
</ol>
</li>
<li><code>make install</code><ol start="2">
<li>创建目录，将可执行文件拷贝到指定目录（安装目录）</li>
<li>加全局可执行路径</li>
<li>加全局的启停脚本</li>
</ol>
</li>
<li><code>make clean</code><ol start="5">
<li>重置编辑环境，删除无关文件</li>
</ol>
</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// main.cpp</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> i=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span> (ture)&#123;</span><br><span class="line">        i++;</span><br><span class="line">        cout&lt;&lt;<span class="string">&quot;main-running-&quot;</span>&lt;&lt;i&lt;&lt;endl;</span><br><span class="line">        <span class="built_in">sleep</span>(<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># ./makefile</span></span><br><span class="line"></span><br><span class="line">TARGET=mian</span><br><span class="line">OBJ:=<span class="variable">$(TARGET)</span>.o</span><br><span class="line"></span><br><span class="line">CC=g++</span><br><span class="line"></span><br><span class="line">PATH=./ins/main/</span><br><span class="line">BIN:=/usr/local/bin/</span><br><span class="line">START_SH:=<span class="variable">$(TARGET)</span>_start</span><br><span class="line">STOP_SH:=<span class="variable">$(TARGET)</span>_stop</span><br><span class="line">LOG:=<span class="variable">$(PATH)</span><span class="variable">$(TARGET)</span>.log</span><br><span class="line"></span><br><span class="line"><span class="variable">$(TARGET)</span>:<span class="variable">$(OBJ)</span></span><br><span class="line"></span><br><span class="line"><span class="section">install:<span class="variable">$(TARGET)</span></span></span><br><span class="line">	if [ -d <span class="variable">$(PATH)</span> ];\</span><br><span class="line">	then echo <span class="variable">$(PATH)</span> exits;\</span><br><span class="line">	<span class="keyword">else</span> \</span><br><span class="line">	mkdir <span class="variable">$(PATH)</span>; \</span><br><span class="line">	cp <span class="variable">$^</span> <span class="variable">$(PATH)</span>;\</span><br><span class="line">	ln -sv <span class="variable">$(PATH)</span><span class="variable">$^</span> <span class="variable">$(BIN)</span>;\</span><br><span class="line">	touch <span class="variable">$(LOG)</span>;\</span><br><span class="line">	chmod a+rmx <span class="variable">$(LOG)</span>;\</span><br><span class="line">	echo -e <span class="string">&quot;<span class="variable">$^</span>&gt;<span class="variable">$(LOG)</span> &amp;\necho <span class="variable">$^</span> running...&quot;</span> &gt; <span class="variable">$(PATH)</span><span class="variable">$(START_SH)</span>;\</span><br><span class="line">	echo -e <span class="string">&quot;killall <span class="variable">$^</span>\necho <span class="variable">$^</span> stoped.&quot;</span> &gt; <span class="variable">$(PATH)</span><span class="variable">$(STOP_SH)</span>;\</span><br><span class="line">	chmod a+X <span class="variable">$(PATH)</span><span class="variable">$(START_SH)</span>;\</span><br><span class="line">	chmod a+X <span class="variable">$(PATH)</span><span class="variable">$(STOP_SH)</span>;\</span><br><span class="line">	ln -sv <span class="variable">$(PATH)</span><span class="variable">$(START_SH)</span> <span class="variable">$(BIN)</span>;\</span><br><span class="line">	ln -sv <span class="variable">$(PATH)</span><span class="variable">$(STOP_SH)</span> <span class="variable">$(BIN)</span>;\</span><br><span class="line">	fi;</span><br><span class="line"></span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">	<span class="variable">$(RM)</span> <span class="variable">$(TARGET)</span> <span class="variable">$(OBJ)</span> <span class="variable">$(BIN)</span><span class="variable">$(TARGET)</span> <span class="variable">$(BIN)</span><span class="variable">$(START_SH)</span> <span class="variable">$(BIN)</span><span class="variable">$(STOP_SH)</span></span><br><span class="line">	<span class="variable">$(RM)</span> -rf <span class="variable">$(PATH)</span></span><br></pre></td></tr></table></figure>

<h2 id="重要记录"><a href="#重要记录" class="headerlink" title="重要记录"></a>重要记录</h2><h3 id="命令的执行"><a href="#命令的执行" class="headerlink" title="命令的执行"></a>命令的执行</h3><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">规则中，当目标需要被重建时。此规则所定义的命令将会被执行，如果是多行命令，那么每一行命令将在一个独立的子 shell 进程中被执行（就是说，每一行命令的执行是在一个独立的 shell 进城中完成）。因此，多行命令之间的执行是相互独立的，相互之间不存在依赖（多条命令行的执行为多个相互独立的进程）。</span><br><span class="line"></span><br><span class="line">在 Makefile 中书写在同一行中的多个命令属于一个完整的 shell 命令行，书写在独立行的一条命令是一个独立的 shell 命令行。因此 ：在一个规则的命令中，命令行“cd”改变目录不会对其后的命令的执行产生影响。就是说其后的命令执行的工作目录不会是之前使用“cd”进入的那个目录。如果要实现这个目的 ”进入的那个目录，就不能把“ 就不能把“cd”和其后的命令放在两行来书写。而应该把这两条命令写在一行上，用分号分隔。这样它们才是一个完整的 shell  命令行。</span><br></pre></td></tr></table></figure>



<h3 id="参考链接"><a href="#参考链接" class="headerlink" title="参考链接"></a>参考链接</h3><font size=2>

<p>[1] <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Xt4y1h7rH?p=1&vd_source=5f03bdbb9a8460c7ae08f9cdea23577b">makefile从入门到项目编译实战</a></p>
<p>[2] <a href="">GunMakefile中文手册</a></p>
</font>


    </div>

    
    
    

      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/blog/tags/makefile/" rel="tag"><i class="fa fa-tag"></i> makefile</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2022/07/11/python%E5%8D%8F%E7%A8%8B/" rel="prev" title="python协程">
      <i class="fa fa-chevron-left"></i> python协程
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2022/07/13/python%E4%B8%8D%E5%90%8C%E6%96%87%E4%BB%B6%E5%A4%B9%E4%B8%8B%E7%9A%84%E5%8C%85%E5%BC%95%E5%85%A5%E9%97%AE%E9%A2%98/" rel="next" title="python不同文件夹下的包引入问题">
      python不同文件夹下的包引入问题 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>


        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#makefile%E7%9B%B8%E5%85%B3%E6%A6%82%E5%BF%B5"><span class="nav-number">1.</span> <span class="nav-text">makefile相关概念</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AE%80%E5%8D%95%E8%BF%90%E8%A1%8C"><span class="nav-number">2.</span> <span class="nav-text">简单运行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#gcc-g-%E7%BC%96%E8%AF%91%E6%B5%81%E7%A8%8B"><span class="nav-number">3.</span> <span class="nav-text">gcc&#x2F;g++编译流程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#makefile%E4%B8%AD%E7%9A%84%E5%8F%98%E9%87%8F"><span class="nav-number">4.</span> <span class="nav-text">makefile中的变量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#makefile%E4%B8%AD%E7%9A%84%E4%BC%AA%E7%9B%AE%E6%A0%87%E5%92%8C%E6%A8%A1%E5%BC%8F%E5%8C%B9%E9%85%8D"><span class="nav-number">5.</span> <span class="nav-text">makefile中的伪目标和模式匹配</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#makefile%E8%BF%90%E8%A1%8C%E6%B5%81%E7%A8%8B"><span class="nav-number">6.</span> <span class="nav-text">makefile运行流程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#makefile%E7%BC%96%E8%AF%91%E5%8A%A8%E6%80%81%E9%93%BE%E6%8E%A5%E5%BA%93"><span class="nav-number">7.</span> <span class="nav-text">makefile编译动态链接库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#makefile%E7%BC%96%E8%AF%91%E9%9D%99%E6%80%81%E9%93%BE%E6%8E%A5%E5%BA%93"><span class="nav-number">8.</span> <span class="nav-text">makefile编译静态链接库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#makefile%E4%B8%AD%E9%80%9A%E7%94%A8%E9%83%A8%E5%88%86%E5%81%9A%E5%85%AC%E5%85%B1%E5%A4%B4%E6%96%87%E4%BB%B6"><span class="nav-number">9.</span> <span class="nav-text">makefile中通用部分做公共头文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#makefile%E4%B8%AD%E8%B0%83%E7%94%A8shell%E5%91%BD%E4%BB%A4"><span class="nav-number">10.</span> <span class="nav-text">makefile中调用shell命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#makefile%E4%B8%AD%E7%9A%84%E5%B5%8C%E5%A5%97%E8%B0%83%E7%94%A8"><span class="nav-number">11.</span> <span class="nav-text">makefile中的嵌套调用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#makefile-%E4%B8%AD%E7%9A%84%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD"><span class="nav-number">12.</span> <span class="nav-text">makefile 中的条件判断</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#makefile%E4%B8%AD%E7%9A%84%E5%BE%AA%E7%8E%AF"><span class="nav-number">13.</span> <span class="nav-text">makefile中的循环</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#makefile%E4%B8%AD%E7%9A%84%E8%87%AA%E5%AE%9A%E4%B9%89%E5%87%BD%E6%95%B0"><span class="nav-number">14.</span> <span class="nav-text">makefile中的自定义函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#make-install%E7%9A%84%E5%AE%9E%E7%8E%B0"><span class="nav-number">15.</span> <span class="nav-text">make install的实现</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%87%8D%E8%A6%81%E8%AE%B0%E5%BD%95"><span class="nav-number"></span> <span class="nav-text">重要记录</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%91%BD%E4%BB%A4%E7%9A%84%E6%89%A7%E8%A1%8C"><span class="nav-number">1.</span> <span class="nav-text">命令的执行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%82%E8%80%83%E9%93%BE%E6%8E%A5"><span class="nav-number">2.</span> <span class="nav-text">参考链接</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="bgape002"
      src="/blog/images/head.png">
  <p class="site-author-name" itemprop="name">bgape002</p>
  <div class="site-description" itemprop="description">mail: bgape002@163.com</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">61</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">66</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2021 – 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">bgape002</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>




  




  
<script src="/blog/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 'szhBf0Qamzsowubi1WnkXmUj-gzGzoHsz',
      appKey     : 'WpwxoK0fVJHvhzWwakr9vbpA',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
